package com.backend.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.dto.ArticleDTO;
import com.common.exception.ModelCoverDTOFailedException;
import com.common.exception.ModelNotFoundException;
import com.common.model.Article;
import com.common.query.ArticleQueryParam;
import com.common.service.ArticleService;
import com.util.CommonPage;
import com.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = "ArticleController", value = "文章管理")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @ApiOperation("文章列表")
    @RequestMapping(value = "/article", method = RequestMethod.GET)
    public CommonResult<CommonPage<ArticleDTO>> get(
            ArticleQueryParam articleQueryParam,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "5") Integer pageSize
    ) {
        IPage<ArticleDTO> article = articleService.findAll(page, pageSize, articleQueryParam);
        return CommonResult.success(CommonPage.restPage(article));
    }

    @ApiOperation("创建文章")
    @RequestMapping(value = "/article", method = RequestMethod.POST)
    public CommonResult create(
            @RequestBody @Valid Article article,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            String error = bindingResult.getAllErrors().get(0).getDefaultMessage();
            return CommonResult.failed(error);
        }

        int result = articleService.saveByForm(article);
        if (result > 0) {
            return CommonResult.success("操作成功");
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取单个文章信息")
    @RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
    public CommonResult getSingleArticle(
            @PathVariable("id") Long id
    ) {
        ArticleDTO article = articleService.findWithUserByArticleId(id);

        return CommonResult.success(article);
    }

    @ApiOperation("更新文章")
    @RequestMapping(value = "/article/{id}", method = RequestMethod.PUT)
    public CommonResult update(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated Article article,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            String error = bindingResult.getAllErrors().get(0).getDefaultMessage();
            return CommonResult.failed(error);
        }

        try {
            boolean result = articleService.edit(id, article);

            if (result) {
                return CommonResult.success("更新成功");
            } else {
                return CommonResult.failed("怎么会更新失败 emmm");
            }

        } catch (ModelNotFoundException e) {
            return CommonResult.failed("找不到要修改的文章哦");
        } catch (ModelCoverDTOFailedException e) {
            return CommonResult.failed("好像提交了非法数据呢");
        }

    }

    @ApiOperation("删除文章")
    @RequestMapping(value = "/article/delete/{id}", method = RequestMethod.DELETE)
    public CommonResult delete(
            @PathVariable("id") long id
    ) {
        articleService.delete(id);
        return CommonResult.success("操作成功");
    }

}
